import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { STColumn, STComponent, STColumnBadge, STColumnTag } from '@delon/abc';
import { SFSchema, SFComponent } from '@delon/form';
import { SupDic, HttpService, GridComponent } from 'lbf';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { YwtxZskxqComponent } from '../zskxq/zskxq.component';
import { Router } from '@angular/router';
import { FlowchartOneItemComponent } from '../../flowchart/one-item/one-item.component';
import { HttpClient } from '@angular/common/http';
import { style } from '@angular/animations';

@Component({
  selector: 'app-ywtx-zskwh',
  templateUrl: './zskwh.component.html',
  styleUrls: ['./zskwh.less']
})
export class YwtxZskwhComponent implements OnInit {
  addFormData = {};
  modiFormData = {};
  queryFormData = {};
  showAddWindow = false;
  showModiWindow = false;

  @ViewChild('st', { static: true }) st: GridComponent;
  @ViewChild('addWindow', { static: false }) addWindow: SFComponent;
  @ViewChild('modiWindow', { static: false }) modiWindow: SFComponent;
  @ViewChild('searchForm', { static: false }) searchForm: SFComponent;

  BADGE: STColumnBadge = {
    1: { text: '企业', color: 'success' },
    2: { text: '自然人', color: 'processing' },
  };


  TAG: STColumnTag = {
    1: { text: '一件事', color: 'green' },
    2: { text: '行政许可', color: 'red' },
    3: { text: '行政审批', color: 'blue' },
    4: { text: '行政给付', color: 'orange' },
  };
  searchSchema: SFSchema = {
    properties: {
      DAD031: {
        type: 'string',
        title: '权力事项名称'
      },
      DGB010: {
        type: 'string',
        title: '所属部门',
        enum: this.dic.getSFDic('MYBM'),
        ui: {
          widget: 'select',
          allowClear:true,
        }
      },

      DAD011: {
        type: 'string',
        title: '事项类型',
        enum: this.dic.getSFDic('DAD011'),
        ui: {
          widget: 'select',
          allowClear:true,
        }
      },
      DAD032: {
        type: 'string',
        title: '对象主体',
        enum: this.dic.getSFDic('DAD032'),
        ui: {
          widget: 'select',
          allowClear:true,
        }
      },
    },
    ui: {
      spanLabelFixed: 100,
      grid: {
        span: 6
      }
    }

  };

  addSchema = {
    properties: {
      DAD017: {
        type: 'string',
        title: '事项主键',
        ui:
        {
          hidden: true
        }

      },
      DAD031: {
        type: 'string',
        title: '事项名称'
      },
      DAD032: {
        type: 'string',
        title: '对象主体',
        enum: this.dic.getSFDic('DAD032'),
        ui: {
          widget: 'radio',

        },
        default: '1'
      },
      DAD011: {
        type: 'string',
        title: '事项类型',
        enum: this.dic.getSFDic('DAD011'),
        ui: {
          widget: 'select',

        }
      },
      DAD004: {
        type: 'string',
        title: '权力编码',
      },
      DGB010: {
        type: 'string',
        title: '所属部门',
        enum: this.dic.getSFDic('MYBM'),
        ui: {
          widget: 'select',

        }
      },

    },
    ui: {
      spanLabelFixed: 90,
      grid: {
        span: 24
      }
    },
    required: ['DAD004', 'DGB010', 'DAD031', 'DAD032', 'DAD011']
  }
  gridData = [];
  columns: STColumn[] = [
    {
      title: '事项状态',
      index: 'DAD023',
      render: 'custom'
    },

    { title: '权力编码', index: 'DAD004' },
    { title: '事项名称', index: 'DAD031' },
    { title: '所属部门', index: 'DGB010', dic: 'DGB010' },
    { title: '事项类型', index: 'DAD011', type: 'tag', tag: this.TAG },
    { title: '对象主体', index: 'DAD032', type: 'badge', badge: this.BADGE },
    {
      title: '操作区', buttons: [
        {
          text: '知识库维护',
          type: 'modal',
          modal: {
            component: YwtxZskxqComponent,
            modalOptions: {nzStyle:{
              top: '15px',
              height:'610px'
            } }
          },
          click: (_record, modal, isVisible) => { },
        },
        {
          text: '修改',
          icon: 'edit',
          click: (record: any) => this.modi(record),
        },
        {
          text: '删除',
          icon: 'delete',
          click: (record: any) => this.del(record),
          pop: true,
          popTitle: '确认要删除该事项吗？',
        },
        {
          text: '流程图',
          click: (record: any) => {
            this.createComponentModal(record.DAD017, record.DAD031)

          },
        }
      ]
    },
  ];

  constructor(private http: HttpClient,
    private dic: SupDic,
    private lbs: HttpService,
    public msgSrv: NzMessageService,
    private router: Router,
    private modalService: NzModalService
  ) { }

  ngOnInit() {

  }

  ngAfterViewInit() {
    this.query();
  }

  add() {
    this.showAddWindow = true;
  }

  query(pi1=false) {
    // this.lbs.lbservice("ZSK_SXCX",{ para: this.searchForm.value }).then(resdata=>{
    //   if(resdata.code<1)
    //   {
    //       this.msgSrv.error(resdata.errmsg);
    //   }
    //   else
    //   {
    //       this.gridData=resdata.message.list;
    //   }

    // });
    if(pi1){
      this.st.pi=1;
    }
    this.st.reload(this.searchForm.value);
  }

  changeSxState(sxbm: number) {
    this.lbs.lbservice("ZSK_SXZT", { para: { DAD017: sxbm } }).then(resdata => {
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }

    });
  }

  modi(rowData: any) {
    this.modiFormData = rowData;
    this.showModiWindow = true;
  }

  del(rowData: any) {
    this.lbs.lbservice("ZSK_SXSC", { para: { DAD017: rowData.DAD017 } }).then(resdata => {
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        // this.showAddWindow = false;
        // this.addFormData = {};
        this.msgSrv.success('事项删除成功');
        this.query(true);
      }
    });
  }

  handleCancel() {
    this.showAddWindow = false;
  }

  handleOk() {

    if (!this.addWindow.valid) {
      this.msgSrv.error('必填项不能为空！');
      return;
    }

    this.lbs.lbservice('ZSK_SXXZ', { para: this.addWindow.value }).then(resdata => {
      console.log(this.addWindow.value);
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.showAddWindow = false;
        this.addFormData = {};
        this.msgSrv.success('事项新增成功');
        this.query();
      }

    })

  }

  modiHandleCancel() {
    this.showModiWindow = false;
  }

  modiHandleOk() {

    if (!this.modiWindow.valid) {
      this.msgSrv.error('必填项不能为空！');
      return;
    }

    this.lbs.lbservice('ZSK_SXXG', { para: this.modiWindow.value }).then(resdata => {
      console.log(this.modiWindow.value);
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.showModiWindow = false;
        this.modiFormData = {};
        this.msgSrv.success('事项修改成功');
        this.query();
      }

    })

  }


  createComponentModal(id: string, name: string): void {
    const modal = this.modalService.create({
      nzTitle: '流程图',
      nzContent: FlowchartOneItemComponent,
      nzWidth: '1000px',
      nzMaskClosable: false,
      nzComponentParams: {
        id,
        name
      },
      nzOkText: null,
    });

    // modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));

    // Return a result when closed
    // modal.afterClose.subscribe(result => console.log('[afterClose] The result is:', result));

    // delay until modal instance created
    setTimeout(() => {
      const instance = modal.getContentComponent();
    }, 2000);
  }
  showFlow = false;
  show() {
    this.showFlow = true
  }
  showCancle() {
    this.showFlow = false
  }
}
